package com.chao.yyks.modular.ranking.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.ListUtile;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.common.util.reaction.PageList;
import com.chao.yyks.modular.ranking.bean.EnRanking;
import com.chao.yyks.modular.ranking.mapper.EnRankingLabelMapper;
import com.chao.yyks.modular.ranking.mapper.EnRankingMapper;
import com.chao.yyks.modular.ranking.service.EnRankingService;
import com.chao.yyks.modular.sys.bean.EnStudent;
import com.chao.yyks.modular.sys.mapper.EnStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 学生排名ServiceImpl
 * chao
 * 2024-09-15
 */
@Service
public class EnRankingServiceImpl implements EnRankingService {

	/**
	 * 学生排名Mapper
	 */
	@Autowired
	private EnRankingMapper enRankingMapper;

	/**
	 * 学生Mapper
	 */
	@Autowired
	private EnStudentMapper enStudentMapper;

	/**
	 * 学生板块排名Mapper
	 */
	@Autowired
	private EnRankingLabelMapper enRankingLabelMapper;

	/**
	 * 新增学生听力时长
	 */
	@Override
	@Transactional
	public void insertEnRanking(EnRanking enRanking){
		if(null == enRanking.getStudentDuration()){
			enRanking.setStudentDuration(0);
		}
		if(null == enRanking.getTedNum()){
			enRanking.setTedNum(0);
		}
		String studentId = RequestUtile.getLoginUserId();
		EnRanking enRankingPojo = enRankingMapper.selectEnRankingByStudentId(studentId);
		if(null == enRankingPojo){
			EnStudent enStudent = enStudentMapper.selectEnStudentByStudentId(studentId);
			if(null == enStudent){
				throw new SysException("登录账号异常,请重新登录或联系老师!");
			}
			enRanking.setStudentId(studentId);
			enRanking.setStudentScore(0D);
			enRanking.setLoginDay(1);
			enRanking.setLoginNum(1);
			enRanking.setClasssId(enStudent.getClasssId());
			enRankingMapper.insertEnRanking(enRanking);
			return;
		}
		enRankingPojo.setStudentDuration(enRankingPojo.getStudentDuration()+enRanking.getStudentDuration());
		enRankingPojo.setTedNum(enRankingPojo.getTedNum()+1);
		enRankingMapper.updateEnRanking(enRankingPojo);
	}

	/**
	 * 查询学生排名详情
	 */
	@Override
	public JSONObject selectEnRankingByStudentId(String studentId){
		EnStudent enStudent = enStudentMapper.selectEnStudentByStudentId(studentId);
		if(null == enStudent){
			throw new SysException("学生不存在!");
		}
		List<EnRanking> rankings = selectEnRankingScore(JSONObject.parseObject("{\"studentId\":\""+studentId+"\",\"classsId\":\""+enStudent.getClasssId()+"\"}"));
		EnRanking ranking = new EnRanking();
		ranking.setStudentDuration(0);
		ranking.setRanking("无");
		ranking.setStudentScore(0D);
		ranking.setLoginDay(0);
		ranking.setLoginNum(0);
		ranking.setTedNum(0);
		if(!ListUtile.isEmpty(rankings)){
			ranking = rankings.get(0);
		}
		JSONObject data = (JSONObject) JSONObject.toJSON(ranking);
		data.put("student",enStudent);
		data.put("typeNumList", enRankingMapper.selectEnTypeNum(studentId));
		data.put("studentNum",enStudentMapper.selectEnStudentNum(enStudent.getClasssId()));
		data.put("scoreAvg",enRankingMapper.selectEnScoreAvg(studentId));
		data.put("wordBankNum",enRankingMapper.selectEnWordBankNum(studentId));
		return data;
	}

	/**
	 * 查询学生排名列表
	 */
	@Override
	public PageList<EnRanking> selectEnRankingByList(JSONObject json){
		return enRankingMapper.selectEnRankingByList(json);
	}

	/**
	 * 分页查询学生排名列表
	 */
	@Override
	public PageList<EnRanking> selectEnRankingByPage(JSONObject json){
		return enRankingMapper.selectEnRankingByList(StrUtile.getPageJson(json));
	}

	/**
	 * 查询得分学生排名
	 */
	@Override
	public List<EnRanking> selectEnRankingAllScore(JSONObject json){
		return enRankingMapper.selectEnRankingAllScore(json);
	}

	/**
	 * 查询得分学生排名
	 */
	@Override
	public List<EnRanking> selectEnRankingScore(JSONObject json){
		return enRankingMapper.selectEnRankingScore(json);
	}

	/**
	 * 查询听力时长学生排名
	 */
	@Override
	public List<EnRanking> selectEnRankingDuration(JSONObject json){
		return enRankingMapper.selectEnRankingDuration(json);
	}

	/**
	 * 查询听力时长学生排名
	 */
	@Override
	public List<EnRanking> selectEnRankingType(JSONObject json){
		return enRankingMapper.selectEnRankingType(json);
	}

}